#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import time
from datetime import datetime, timedelta

from db import DevDataOperator
from utils.ConfigInfo import ConfigLoader
from plugin import AbnormalDataFiltering
from plugin import DataAlignment

class DevDataProcessingService:
    """
    设备数据处理服务

    @Author: kindey
    @Date: 2025/6/19
    @Description:
    """

    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)

    def __init__(self):
        ConfigLoader.load_config()
        self.conf = ConfigLoader.get_config()
        pass
    def pro_one_dev_data(self, dev_id: int):
        """
        处理单个设备的数据

        :param dev_id 设备id
        """
        z_score_threshold = 3
        DB_PATH_HSJ = self.conf['path']['db']+'火石咀煤矿(2024.12.30).db'
        DB_PATH_CUSTOM = self.conf['path']['db']+'custom.db'
        dev_data_opt = DevDataOperator(DB_PATH_HSJ, DB_PATH_CUSTOM)

        total_begin_time = time.time() * 1000
        self.logger.info(f"{total_begin_time}====>>开始处理数据...")
        block_begin_time = time.time() * 1000
        self.logger.info(f"{block_begin_time}====>>原始数据转存开始。")
        # 保存原始数据
        dev_data_list = dev_data_opt.query_dev_data(dev_id)
        dev_data_opt.save_dev_data(dev_id, None, dev_data_list)
        block_end_time = time.time() * 1000
        self.logger.info(f"{block_end_time}====>>原始数据转存完成，耗时：{block_end_time - block_begin_time}。")

        # IQR筛选并保存筛选后数据
        block_begin_time = time.time() * 1000
        self.logger.info(f"{block_begin_time}====>>IQR异常数据过滤开始")
        iqr_dev_data = AbnormalDataFiltering.iqr_filtering(dev_data_list)
        dev_data_opt.save_dev_data(dev_id, 'iqr', iqr_dev_data)
        block_end_time = time.time() * 1000
        self.logger.info(f"{block_end_time}====>>IQR异常数据过滤完成，耗时：{block_end_time - block_begin_time}。")

        # Z-score数据过滤
        if dev_id == 487 or dev_id == 492:
            # 一氧化碳的数据不能使用Z-score进行数据过滤
            zscore_dev_data = iqr_dev_data
        else:
            block_begin_time = time.time() * 1000
            self.logger.info(f"{block_begin_time}====>>Z-score异常数据过滤开始")
            zscore_dev_data = AbnormalDataFiltering.z_score_filtering(iqr_dev_data, z_score_threshold)
            dev_data_opt.save_dev_data(dev_id, 'zsc', zscore_dev_data)
            block_end_time = time.time() * 1000
            self.logger.info(f"{block_end_time}====>>Z-score过滤数据完成，耗时：{block_end_time - block_begin_time}。")

        # 按20分钟每次做数据对齐
        block_begin_time = time.time() * 1000
        self.logger.info(f"{block_begin_time}====>>数据对齐重采开始")
        # 获取时间范围
        start_time = datetime.strptime('2024-11-06 00:00:00', "%Y-%m-%d %H:%M:%S")
        end_time = datetime.strptime('2024-12-30 23:59:59', "%Y-%m-%d %H:%M:%S")
        time_delta = timedelta(minutes=20)
        aligned_dev_date = DataAlignment.reacquisition_by_time(zscore_dev_data, start_time, end_time, time_delta)
        dev_data_opt.save_dev_data(dev_id, 'rec', aligned_dev_date)
        block_end_time = time.time() * 1000
        self.logger.info(f"{block_end_time}====>>数据对齐重采完成，耗时：{block_end_time - block_begin_time}。")
        self.logger.info(f"{total_begin_time}====>>结束处理数据，耗时：{block_end_time - total_begin_time}。")

    def query_mult_dimension_dev_data(self):
        """
        查询多维设备数据
        :return: 多维设备数据
        """
        DB_PATH_HSJ = self.conf['path']['db']+'火石咀煤矿(2024.12.30).db'
        DB_PATH_CUSTOM = self.conf['path']['db']+'custom.db'
        dev_data_opt = DevDataOperator(DB_PATH_HSJ, DB_PATH_CUSTOM)
        mtul_dev_data = dev_data_opt.query_mult_dimension_dev_data()
        return mtul_dev_data

    def get_mult_dev_columns(self):
        return [
                    'data_time', 'jia_wan', 'wen_du', 'yi_yang', 'feng_su', 'su_du',
                    'ya_li_68', 'ya_li_69', 'ya_li_70',
                    'ya_li_71', 'ya_li_72', 'ya_li_73', 'ya_li_74', 'ya_li_75', 'ya_li_76', 'ya_li_77', 'ya_li_78', 'ya_li_79', 'ya_li_80',
                    'ya_li_81', 'ya_li_82', 'ya_li_83', 'ya_li_84', 'ya_li_85', 'ya_li_86', 'ya_li_87', 'ya_li_88', 'ya_li_89', 'ya_li_90',
                    'ya_li_91', 'ya_li_92', 'ya_li_93', 'ya_li_94', 'ya_li_95', 'ya_li_96', 'ya_li_97', 'ya_li_98', 'ya_li_99', 'ya_li_100',
                    'ya_li_101', 'ya_li_102', 'ya_li_103', 'ya_li_104', 'ya_li_105', 'ya_li_106', 'ya_li_107', 'ya_li_108', 'ya_li_109', 'ya_li_110',
                    'ya_li_111', 'ya_li_112', 'ya_li_113', 'ya_li_114', 'ya_li_115', 'ya_li_116', 'ya_li_117', 'ya_li_118', 'ya_li_119', 'ya_li_120',
                    'ya_li_121', 'ya_li_122', 'ya_li_123', 'ya_li_124', 'ya_li_125', 'ya_li_126', 'ya_li_127', 'ya_li_128', 'ya_li_129', 'ya_li_130',
                    'ya_li_131', 'ya_li_132', 'ya_li_133', 'ya_li_134', 'ya_li_135', 'ya_li_136', 'ya_li_137', 'ya_li_138', 'ya_li_139', 'ya_li_140',
                    'ya_li_141', 'ya_li_142', 'ya_li_143', 'ya_li_144', 'ya_li_145', 'ya_li_146', 'ya_li_147', 'ya_li_148', 'ya_li_149', 'ya_li_150',
                    'ya_li_151', 'ya_li_152', 'ya_li_153', 'ya_li_154', 'ya_li_155', 'ya_li_156', 'ya_li_157', 'ya_li_158', 'ya_li_159', 'ya_li_160',
                    'ya_li_161', 'ya_li_162', 'ya_li_163', 'ya_li_164', 'ya_li_165', 'ya_li_166', 'ya_li_167', 'ya_li_168', 'ya_li_169', 'ya_li_170',
                    'ya_li_171', 'ya_li_172', 'ya_li_173', 'ya_li_174', 'ya_li_175', 'ya_li_176', 'ya_li_177', 'ya_li_178', 'ya_li_179', 'ya_li_180',
                    'ya_li_181', 'ya_li_182', 'ya_li_183', 'ya_li_184', 'ya_li_185', 'ya_li_186', 'ya_li_187', 'ya_li_188', 'ya_li_189', 'ya_li_190',
                    'ya_li_191', 'ya_li_192', 'ya_li_193', 'ya_li_194', 'ya_li_195', 'ya_li_196', 'ya_li_197', 'ya_li_198',
                    'xing_cheng_267'
                ]